
import conf from '../config/userconf.js'
export default{
    data(){
        return{
            list: [],  //分类列表数据
            current: 1,  //分类当前页码
            dialogTableVisible: false,   //嵌套表格隐藏展示
            gridData: [],  //种类列表数据
            typeCurrent: 1,  //种类当前页码
            t_id: 1,  // 当前的分类id
            t_name: '',  // 当前的分类名称
            searchData: {
                searchName: '',  //查询分类名称
            }, 
            searchzlData: {
                searchName: '',  //查询种类名称
            }, 
        }
    },
    filters:{

    },
    methods:{
        /* 分类列表*/
        getList (data=Object()){
            if(data.searchName){
                data.t_name = data.searchName
            }
            data.page = this.current;
            conf.type.list(data,(state,res)=>{
                if (state == 'success'&&res.data.code == 0){
                    // console.log(res);
                    this.list = res.data.data;
                }else if(state == 'success'&&res.data.code != 0){
                    // console.log(res);
                    this.$message({
                        type: 'info',
                        message: res.data.msg
                    });
                }
            })
        },
        // 添加分类
        add(data=null){
            this.$prompt('添加分类', {
                inputPlaceholder:'请输入分类名称',
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(({ value }) => {
                data.t_name = value;
                conf.type.add(data,(state,res)=>{
                    if(state == 'success'&&res.data.code == 0){
                        // console.log(res);
                        this.getList();
                        this.$message({
                            type: 'success',
                            message: '添加成功'
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消添加'
                });       
            });

        },
        // 查看分类下种类
        look(row,data=Object()){
            this.t_id = row.t_id;
            this.t_name = row.t_name;
            this.typeCurrent = 1;
            this.getTypeList();
            this.dialogTableVisible = true;
            
        },
        // 删除分类
        del(row,data=Object()){
           
            this.$confirm('确认删除分类 "'+row.t_name+'" ?', '删除分类', {  
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {

                data.t_id = row.t_id;
                conf.type.del(data,(state,res)=>{
                    if(state == 'success'&&res.data.code == 0){
                        this.getList();
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                          });
                    }else if(state == 'success'&&res.data.code != 0){
                        this.$message({
                            type: 'info',
                            message: res.data.msg
                        });
                    }
                })

            }).catch(() => {
                this.$message({
                  type: 'info',
                  message: '已取消删除'
                });          
            });
        },
        // 修改分类
        edit(row,data=Object()){
            this.$prompt('修改分类', {
                inputPlaceholder:'请输入分类名称',
                inputValue: row.t_name,
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(({ value }) => {
                data.t_name = value;
                data.t_id = row.t_id;
                conf.type.edit(data,(state,res)=>{
                    if(state == 'success'&&res.data.code == 0){
                        // console.log(res);
                        this.getList();
                        this.$message({
                            type: 'success',
                            message: '修改成功'
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消修改'
                });       
            });

        },
        // 分类 切换页码
        handleCurrentChange(val) {
            this.current = val;
            this.getList();
        },
        // 种类 切换页码
        handletypeCurrentChange(val) {
            this.typeCurrent = val;
            this.getTypeList();
        },
        /* 种类列表*/
        getTypeList (data=Object()){

            data.page = this.typeCurrent;
            data.td_id = this.t_id;
            conf.type.typeList(data,(state,res)=>{
                if (state == 'success'&&res.data.code == 0){
                    // console.log(res);
                    this.gridData = res.data.data;
                }else if(state == 'success'&&res.data.code != 0){
                    // console.log(res);
                    this.gridData = [];
                    this.$message({
                        type: 'info',
                        message: res.data.msg
                    });
                }
            })
        },
        // 添加种类
        typeAdd(data=null){
            this.$prompt('添加种类', {
                inputPlaceholder:'请输入种类名称',
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(({ value }) => {
                data.td_tid = this.t_id;  //分类id
                data.td_name = value;
                conf.type.typeAdd(data,(state,res)=>{
                    // console.log(res);
                    if(state == 'success'&&res.data.code == 0){
                        // console.log(res);
                        this.getTypeList();
                        this.$message({
                            type: 'success',
                            message: '添加成功'
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消添加'
                });       
            });
            

        },
        // 删除种类
        typeDel(row,data=Object()){
           
            this.$confirm('确认删除种类 "'+row.td_name+'" ?', '删除种类', {  
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                data.td_tid = this.t_id;    //分类id
                data.td_id = row.td_id;    //种类id
                conf.type.typeDel(data,(state,res)=>{
                    if(state == 'success'&&res.data.code == 0){
                        // console.log(res);
                        this.getTypeList();
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    }else if(state == 'success'&&res.data.code != 0){
                        this.$message({
                            type: 'info',
                            message: res.data.msg
                        });
                    }
                })

            }).catch(() => {
                this.$message({
                  type: 'info',
                  message: '已取消删除'
                });          
            });
        },
        // 修改种类
        typeEdit(row,data=Object()){
            // console.log(row);
            this.$prompt('修改种类', {
                inputPlaceholder:'请输入种类名称',
                inputValue: row.td_name,
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(({ value }) => {
                data.td_name = value;
                data.td_tid = this.t_id;    //分类id
                data.td_id = row.td_id;    //种类id
                conf.type.typeEdit(data,(state,res)=>{
                    if(state == 'success'&&res.data.code == 0){
                        // console.log(res);
                        this.getTypeList();
                        this.$message({
                            type: 'success',
                            message: '修改成功'
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消修改'
                });       
            });

        },
        //查询分类
        search(){
            let data  = this.searchData;
            this.getList(data);
        },
        //查询种类
        searchzl(){
            let data  = this.searchzlData;
            this.getTypeList(data);
        },
    },
    mounted (){
        this.$nextTick(function(){
            this.getList();
            this.getTypeList();
        })
    },
    // 计算属性
    computed :{
        
    }
}